package lc20240515;

//48.旋转图像
public class Demo1 {
    public static void main(String[] args) {
       int[][] matrix={{1,2,3},{4,5,6},{7,8,9}};
       rotate(matrix);
       printMatrix(matrix);
    }
    public static void rotate(int[][] matrix) {
        int row=matrix.length;
        int col=matrix[0].length;
        for (int i = 0; i < row; i++) {
            for (int j = i; j < col; j++) {
                int temp=matrix[i][j];
                matrix[i][j]=matrix[j][i];
                matrix[j][i]=temp;
            }
        }

        //反转每一行
        for (int i = 0; i < row; i++) {
            int left=0;
            int right=col-1;
            while(left<right){
                int temp=matrix[i][left];
                matrix[i][left]=matrix[i][right];
                matrix[i][right]=temp;
                left++;
                right--;
            }
        }
    }
    public static void printMatrix(int[][] matrix) {
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();  // 换行
        }
    }
}
